package github.leavesczy.reactivehttpsamples.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import github.leavesczy.reactivehttp.base.ReactiveFragment
import github.leavesczy.reactivehttpsamples.util.Logger
import github.leavesczy.reactivehttpsamples.widget.dialog.LoadDialog

/**
 * @Author: leavesCZY
 * @Date: 2022/4/23 20:38
 * @Desc:
 * @Github：https://github.com/leavesCZY
 */
open class BaseFragment : ReactiveFragment(){

    private var myLoadDialog:LoadDialog?=null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Logger.d("Fragment onCreateView-->" + this.javaClass.simpleName)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Logger.d("Fragment onViewCreated-->" + this.javaClass.simpleName)
    }

    override fun onResume() {
        super.onResume()
        Logger.d("Fragment onResume-->" + this.javaClass.simpleName)
    }

    override fun onPause() {
        super.onPause()
        Logger.d("Fragment onPause-->" + this.javaClass.simpleName)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Logger.d("Fragment onDestroyView-->" + this.javaClass.simpleName)
    }

    override fun showLoading(msg:String,cancelable:Boolean) {
        activity?.let { act ->
            myLoadDialog = LoadDialog(act,!cancelable,msg).apply {
                setCancelable(cancelable)
                setCanceledOnTouchOutside(cancelable)
                show()
            }
        }
        Logger.d("Fragment showLoading-->" + this.javaClass.simpleName)
    }

    override fun dismissLoading() {

        myLoadDialog?.takeIf { it.isShowing }?.dismiss()
        myLoadDialog = null
        Logger.d("Fragment dismissLoading-->" + this.javaClass.simpleName)
    }


    protected fun showView(view: View,show:Boolean=true){
        view.visibility=if(show) View.VISIBLE else View.GONE
    }
}